---
title: 'Section 5: Election Surveys'
author: "Milan Svolik"
date: "4/20/2023"
output:
  pdf_document: default
  html_document:
    df_print: paged
---

```{r setup, include=FALSE}
rm(list = ls(all = TRUE))
library(tidyverse)
library(stargazer)
library(lmtest)
library(reshape2)
library(foreign)
library(ggplot2)
library(broom)
library(estimatr)
library(texreg)


set.seed(5877)
```

## Load the merged survey data 
* Two proprietary surveys conducted by the survey agencies KONDA and SONAR
```{R}
load("data/df_surveys.RData")
```

# Table 5.2
* Alternative post-stratification schemes for the 2019 mayoral election surveys


### CHECK 2019 MARGINS
* note that the drop_na results in a dataset that has BOTH vote_march_3 and vote_june_3 available
```{R}
library(weights)

# RAW DATA
df_surveys_temp <- df_surveys %>%
  select(vote_march_3, vote_june_3) %>%
  drop_na()

df_table <- data.frame(t(round(100*wpct(df_surveys_temp$vote_march_3), 2)), t(round(100*wpct(df_surveys_temp$vote_june_3), 2)))

# DEMOGRAPHICS ONLY
df_surveys_temp <- df_surveys %>%
  select(vote_march_3, vote_june_3, weight=weight_s2) %>%
  drop_na()

df_table <- bind_rows(df_table,
  data.frame(t(round(100*wpct(df_surveys_temp$vote_march_3, weight = df_surveys_temp$weight), 2)), t(round(100*wpct(df_surveys_temp$vote_june_3, weight = df_surveys_temp$weight), 2))))

# 2018 LEGISLATIVE ONLY
df_surveys_temp <- df_surveys %>%
  select(vote_march_3, vote_june_3, weight=weight_s3) %>%
  drop_na()

df_table <- bind_rows(df_table,
  data.frame(t(round(100*wpct(df_surveys_temp$vote_march_3, weight = df_surveys_temp$weight), 2)), t(round(100*wpct(df_surveys_temp$vote_june_3, weight = df_surveys_temp$weight), 2))))

# 2019 MAYORAL ONLY
df_surveys_temp <- df_surveys %>%
  select(vote_march_3, vote_june_3, weight=weight_s4) %>%
  drop_na()

df_table <- bind_rows(df_table,
  data.frame(t(round(100*wpct(df_surveys_temp$vote_march_3, weight = df_surveys_temp$weight), 2)), t(round(100*wpct(df_surveys_temp$vote_june_3, weight = df_surveys_temp$weight), 2))))

# 2018 LEGISLATIVE AND 2019 MAYORAL
df_surveys_temp <- df_surveys %>%
  select(vote_march_3, vote_june_3, weight=weight_s1) %>%
  drop_na()

df_table <- bind_rows(df_table,
  data.frame(t(round(100*wpct(df_surveys_temp$vote_march_3, weight = df_surveys_temp$weight), 2)), t(round(100*wpct(df_surveys_temp$vote_june_3, weight = df_surveys_temp$weight), 2))))

# DEMOGRAPHICS, 2018 LEGISLATIVE AND 2019 MAYORAL
df_surveys_temp <- df_surveys %>%
  select(vote_march_3, vote_june_3, weight=weight_s5) %>%
  drop_na()

df_table <- bind_rows(df_table,
  data.frame(t(round(100*wpct(df_surveys_temp$vote_march_3, weight = df_surveys_temp$weight), 2)), t(round(100*wpct(df_surveys_temp$vote_june_3, weight = df_surveys_temp$weight), 2))))
```

### Report Table 5.2
```{R}
colnames(df_table) <- c("AKP March", "CHP March", "ABS March", "AKP June", "CHP June", "ABS June")
df_table
```

# Table 5.3
* The three mechanisms of punishment under alternative classification schemes for undecided and third-party voters
```{R}
# Clear memory
rm(list = ls(all = TRUE))

# Load libraries
library(tidyverse)
library(stargazer)
library(lmtest)
library(reshape2)
library(foreign)
library(ggplot2)
library(estimatr)
library(kableExtra)
library(modelr)
library(modelsummary)
library(survey)

# Display code but not warnings along with its results in the knitted file
knitr::opts_chunk$set(echo = TRUE, warning = F, message = F)
options(scipen=999)

set.seed(5877)
load("data/df_surveys.RData")
```

## PREPARE ALL OF THE DATA THAT WILL BE NEEDED FOR RAKING
* We must re-rake under each classification because the number of ABSTAINERS will be different depending on how we treat the UNDECIDED
* Convert all raking vars to character vectors
```{R}
# vote_march: AKP, CHP, THIRD, ABS
df_surveys$vote_march2019_torake <- as.character(df_surveys$vote_march)
str(df_surveys$vote_march2019_torake)
table(df_surveys$vote_march2019_torake)

# vote_2018: AKP, CHP, MHP, HDP, IYI, SAADET, OTHER, ABS
df_surveys$vote_2018_torake <- as.character(df_surveys$vote_2018)
```

### Recode choices in election results so that they march the coding of the survey variables
* The file "df_rake_istanbul.RData" contains official election margins from the YSK website 
* Coding: 
  + vote_march: ABS, AKP, CHP, THIRD
  + vote_june: ABS, AKP, CHP, THIRD
  + vote_2018: ABS, AKP, CHP, HDP, IYI, MHP, OTHER, SAADET
```{R}
load("data/df_rake_istanbul.RData")
```

### March 2019
* vote_march: ABS, AKP, CHP, THIRD
```{R}
results_march2019_torake <- results_mrp %>% 
  select(party, `2019, March, metropolitan mayor`) %>%
  rename(vote_march2019_torake = `2019, March, metropolitan mayor`) %>%
  filter(party!= "contested", party!= "invalid", party!= "uncontested", party!= "valid", party!= "voted") %>%
  filter(!is.na(vote_march2019_torake))

table(results_march2019_torake$party)
results_march2019_torake$party <- ifelse(results_march2019_torake$party=="BTP"|
                                      results_march2019_torake$party=="DP"|
                                      results_march2019_torake$party=="DSP"|
                                      results_march2019_torake$party=="SP"|
                                      results_march2019_torake$party=="TKP"|
                                      results_march2019_torake$party=="VP",
                                      "THIRD", results_march2019_torake$party)

table(results_march2019_torake$party)

results_march2019_torake <- results_march2019_torake %>%
  select(party, vote_march2019_torake) %>%
  group_by(party) %>%
  summarise(count = sum(vote_march2019_torake, na.rm = TRUE))

reg_torake <- results_march2019_torake$count[results_march2019_torake$party=="registered"]

results_march2019_torake <- results_march2019_torake %>%
  filter(party!="registered")

count_torake <- sum(results_march2019_torake$count)

results_march2019_torake <- results_march2019_torake %>%
  add_row(party="ABS", count=reg_torake-count_torake) %>%
  arrange(party)

results_march2019_torake <- results_march2019_torake %>%
  rename(vote_march2019_torake=party)

results_march2019_torake
```

### June 2019
* vote_june: ABS, AKP, CHP, THIRD
```{R}
results_june2019_torake <- results_mrp %>% 
  select(party, `2019, June, metropolitan mayor`) %>%
  rename(vote_june2019_torake = `2019, June, metropolitan mayor`) %>%
    filter(party!= "contested", party!= "invalid", party!= "uncontested", party!= "valid", party!= "voted") %>%
  filter(!is.na(vote_june2019_torake))

table(results_june2019_torake$party)
results_june2019_torake$party <- ifelse(results_june2019_torake$party=="SP"|
                                      results_june2019_torake$party=="VP",
                                      "THIRD", results_june2019_torake$party)

table(results_june2019_torake$party)

results_june2019_torake <- results_june2019_torake %>%
  select(party, vote_june2019_torake) %>%
  group_by(party) %>%
  summarise(count = sum(vote_june2019_torake, na.rm = TRUE))

reg_torake <- results_june2019_torake$count[results_june2019_torake$party=="registered"]

results_june2019_torake <- results_june2019_torake %>%
  filter(party!="registered")

count_torake <- sum(results_june2019_torake$count)

results_june2019_torake <- results_june2019_torake %>%
  add_row(party="ABS", count=reg_torake-count_torake) %>%
  arrange(party)

results_june2019_torake <- results_june2019_torake %>%
  rename(vote_june2019_torake=party)

results_june2019_torake
```

### 2018 Legislative
* vote_2018: ABS, AKP, CHP, HDP, IYI, MHP, OTHER, SAADET
```{r}
names(table(results_mrp$party))
results_2018_torake <- results_mrp %>% 
  filter(party!= "contested", party!= "invalid", party!= "uncontested", party!= "valid", party!= "voted")

table(results_2018_torake$party)
results_2018_torake$party <- ifelse(results_2018_torake$party=="VP"|
                                      results_2018_torake$party=="BTP"|
                                      results_2018_torake$party=="TKP"|
                                      results_2018_torake$party=="BBP"|
                                      results_2018_torake$party=="DP"|
                                      results_2018_torake$party=="DSP"| 
                                      results_2018_torake$party=="HUDA"|
                                      results_2018_torake$party=="INDEPENDENT",  
                                      "OTHER", results_2018_torake$party)

results_2018_torake$party <- ifelse(results_2018_torake$party=="SP", "SAADET", results_2018_torake$party)

table(results_2018_torake$party)

results_2018_torake <- results_2018_torake %>%
  select(party, `2018 parliamentary`) %>%
  group_by(party) %>%
  summarise(count = sum(`2018 parliamentary`, na.rm = TRUE))

reg_torake <- results_2018_torake$count[results_2018_torake$party=="registered"]

results_2018_torake <- results_2018_torake %>%
  filter(party!="registered")

count_torake <- sum(results_2018_torake$count)

results_2018_torake <- results_2018_torake %>%
  add_row(party="ABS", count=reg_torake-count_torake) %>%
  arrange(party)

# COMPARE CATEGORIES
sort(names(table(df_surveys$vote_2018_torake, useNA = c("always"))))
table(results_2018_torake$party, useNA = c("always"))

results_2018_torake <- results_2018_torake %>%
  rename(vote_2018_torake=party)

results_2018_torake
```

### Load data with administrative data on demographics
```{R}
load("data/df_admin_istanbul.RData")

### Survey Demographics: 
df_surveys_torake <- df_surveys %>%
  filter(!is.na(dg_sex) & !is.na(dg_age) & !is.na(dg_edu_6)) %>%
  mutate(demography_char = as.character(paste(dg_sex, dg_age, dg_edu_6))) 
table(df_surveys_torake$demography_char)

nrow(df_surveys_torake)

# Compare to the admin data
demography_surveys <- data.frame(sort(names(table(df_surveys_torake$demography_char)))) 
demography_admin <- data.frame(sort(admin_mrp$demography))
colnames(demography_surveys) <- c("demography")
colnames(demography_admin) <- c("demography")
str(demography_admin$demography)
str(demography_surveys$demography)

# THE FACTORS MUST BE TURNED INTO CHARACTERS
demography_surveys$demography_char <- as.character(demography_surveys$demography)
demography_admin$demography_char <- as.character(demography_admin$demography)

admin_mrp <- admin_mrp %>%
  mutate(demography_char = demography_admin$demography_char)

demography_surveys <- demography_surveys %>%
  select(demography_char) %>%
  mutate(id = row_number())

admin_temp <- admin_mrp %>%
  full_join(demography_surveys)

admin_temp %>% 
  filter(is.na(id))

admin_torake <- admin_temp %>%
  filter(!is.na(id)) %>%
  select(count, demography_char)

dim(admin_torake)
dim(df_surveys_torake)
```

## Classification 1: Third-party voters to ABS, June undecided DROPPED
```{R}
table(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- as.character(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- ifelse(df_surveys$vote_june2019_torake=="Undecided", NA, df_surveys$vote_june2019_torake)
str(df_surveys$vote_june2019_torake)

df_surveys_torake <- df_surveys %>%
  filter(!is.na(dg_sex) & !is.na(dg_age) & !is.na(dg_edu_6)) %>%
  filter(!is.na(vote_2018_torake) & !is.na(vote_march2019_torake) & !is.na(vote_june2019_torake))

df_torake <- svydesign(ids=~1, data=df_surveys_torake)

df_raked <- rake(df_torake, list(~demography_char, ~vote_2018_torake, ~vote_march2019_torake, ~vote_june2019_torake), list(admin_torake, results_2018_torake, results_march2019_torake, results_june2019_torake), control = list(maxit = 100, epsilon = 1, verbose=FALSE))

df_weights <- data.frame(rid = df_surveys_torake$rid, weight_robustness=weights(df_raked)) 

df_surveys_robustness <- df_surveys_torake %>%
  left_join(df_weights)

df_surveys_robustness$vote_march_3 <- fct_recode(df_surveys_robustness$vote_march, 
                            ABS = "THIRD")
df_surveys_robustness$vote_june_3 <- fct_recode(df_surveys_robustness$vote_june, 
                            ABS = "THIRD",
                            NULL ="Undecided")

# Use post-stratification weights as boostrap probabilities
df_toboot <- df_surveys_robustness %>%
  mutate(weight = weight_robustness) %>%
  filter(!is.na(weight)) %>%
  mutate(rid = seq(1:n())) %>%
  select(rid, vote_march_3, vote_june_3, vote_march, vote_june, weight)

# CHECK THAT THE THIRD/UNDECIDED ARE RE-CLASSIFIED AS INTENDED
table(df_toboot$vote_march, df_toboot$vote_march_3, useNA = c("always"))
table(df_toboot$vote_june, df_toboot$vote_june_3, useNA = c("always"))

# BOOTSTRAP 
boot_rep <- 10000
mechs_out <- c()
p_boot_save <- c()
p_counts_boot_save <- c()
for (j in 1:boot_rep) {
  # DRAW RIDS
  rid_boot <- sample(df_toboot$rid, replace=TRUE, prob=df_toboot$weight)
  # EXTRACT THE DATA BASED ON THE DRAW
  df_boot <- df_toboot[rid_boot,]
  # THE CROSSTAB
  p_counts_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)
  # COMPUTE THE JOINT PMF
  p_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)/sum(table(df_boot$vote_march_3, df_boot$vote_june_3))
  # TRANSFORM TO A VECTOR THAT IS A ROWWISE TRANSFORMATION OF THE MATRIX
  p_counts_boot <- c(t(p_counts_boot))
  p_boot <- c(t(p_boot))
  # SAVE IT
  p_counts_boot_save <- rbind(p_counts_boot_save, p_counts_boot)
  p_boot_save <- rbind(p_boot_save, p_boot)
}
  
### Compute the mechanisms: the JOINT PMF
switching_boot <- c()
backlash_boot <- c()
disenchantment_boot <- c()
  
for (k in 1:boot_rep) {
  P <- matrix(p_boot_save[k,], nrow=3, byrow=TRUE)
  
  switching <- P[1,2] - P[2,1]
  switching_boot <- rbind(switching_boot, switching)
  
  backlash <- P[3,2] - P[2,3]
  backlash_boot <- rbind(backlash_boot, backlash)
  
  disenchantment <- P[1,3] - P[3,1]
  disenchantment_boot <- rbind(disenchantment_boot, disenchantment)
}

switching_boot_mean <- apply(switching_boot, 2, mean, na.rm=TRUE)
switching_boot_lower <- apply(switching_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
switching_boot_upper <- apply(switching_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

backlash_boot_mean <- apply(backlash_boot, 2, mean, na.rm=TRUE)
backlash_boot_lower <- apply(backlash_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
backlash_boot_upper <- apply(backlash_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

disenchantment_boot_mean <- apply(disenchantment_boot, 2, mean, na.rm=TRUE)
disenchantment_boot_lower <- apply(disenchantment_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
disenchantment_boot_upper <- apply(disenchantment_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

# CONSOLIDATE
switching_boot_out <- c(switching_boot_mean, switching_boot_lower, switching_boot_upper)
backlash_boot_out <- c(backlash_boot_mean, backlash_boot_lower, backlash_boot_upper)
disenchantment_boot_out <- c(disenchantment_boot_mean, disenchantment_boot_lower, disenchantment_boot_upper)

df_table <- data.frame(t(round(100*switching_boot_out, 3)), t(round(100*backlash_boot_out, 3)), t(round(100*disenchantment_boot_out, 3)))
```

## Classification 2: Third-party voters to ABS, June undecided to ABS  
```{R}
table(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- as.character(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- ifelse(df_surveys$vote_june2019_torake=="Undecided", "ABS", df_surveys$vote_june2019_torake)
str(df_surveys$vote_june2019_torake)

df_surveys_torake <- df_surveys %>%
  filter(!is.na(dg_sex) & !is.na(dg_age) & !is.na(dg_edu_6)) %>%
  filter(!is.na(vote_2018_torake) & !is.na(vote_march2019_torake) & !is.na(vote_june2019_torake))

df_torake <- svydesign(ids=~1, data=df_surveys_torake)

df_raked <- rake(df_torake, list(~demography_char, ~vote_2018_torake, ~vote_march2019_torake, ~vote_june2019_torake), list(admin_torake, results_2018_torake, results_march2019_torake, results_june2019_torake), control = list(maxit = 100, epsilon = 1, verbose=FALSE))

df_weights <- data.frame(rid = df_surveys_torake$rid, weight_robustness=weights(df_raked)) 

df_surveys_robustness <- df_surveys_torake %>%
  left_join(df_weights)

df_surveys_robustness$vote_march_3 <- fct_recode(df_surveys_robustness$vote_march, 
                            ABS = "THIRD")
df_surveys_robustness$vote_june_3 <- fct_recode(df_surveys_robustness$vote_june, 
                            ABS = "THIRD",
                            ABS ="Undecided")

# Use post-stratification weights as boostrap probabilities
df_toboot <- df_surveys_robustness %>%
  mutate(weight = weight_robustness) %>%
  filter(!is.na(weight)) %>%
  mutate(rid = seq(1:n())) %>%
  select(rid, vote_march_3, vote_june_3, vote_march, vote_june, weight)

# CHECK THAT THE THIRD/UNDECIDED ARE RE-CLASSIFIED AS INTENDED
table(df_toboot$vote_march, df_toboot$vote_march_3, useNA = c("always"))
table(df_toboot$vote_june, df_toboot$vote_june_3, useNA = c("always"))

# BOOTSTRAP 
boot_rep <- 10000
mechs_out <- c()
p_boot_save <- c()
p_counts_boot_save <- c()
for (j in 1:boot_rep) {
  # DRAW RIDS
  rid_boot <- sample(df_toboot$rid, replace=TRUE, prob=df_toboot$weight)
  # EXTRACT THE DATA BASED ON THE DRAW
  df_boot <- df_toboot[rid_boot,]
  # THE CROSSTAB
  p_counts_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)
  # COMPUTE THE JOINT PMF
  p_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)/sum(table(df_boot$vote_march_3, df_boot$vote_june_3))
  # TRANSFORM TO A VECTOR THAT IS A ROWWISE TRANSFORMATION OF THE MATRIX
  p_counts_boot <- c(t(p_counts_boot))
  p_boot <- c(t(p_boot))
  # SAVE IT
  p_counts_boot_save <- rbind(p_counts_boot_save, p_counts_boot)
  p_boot_save <- rbind(p_boot_save, p_boot)
}
  
### Compute the mechanisms: the JOINT PMF
switching_boot <- c()
backlash_boot <- c()
disenchantment_boot <- c()
  
for (k in 1:boot_rep) {
  P <- matrix(p_boot_save[k,], nrow=3, byrow=TRUE)
  
  switching <- P[1,2] - P[2,1]
  switching_boot <- rbind(switching_boot, switching)
  
  backlash <- P[3,2] - P[2,3]
  backlash_boot <- rbind(backlash_boot, backlash)
  
  disenchantment <- P[1,3] - P[3,1]
  disenchantment_boot <- rbind(disenchantment_boot, disenchantment)
}

switching_boot_mean <- apply(switching_boot, 2, mean, na.rm=TRUE)
switching_boot_lower <- apply(switching_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
switching_boot_upper <- apply(switching_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

backlash_boot_mean <- apply(backlash_boot, 2, mean, na.rm=TRUE)
backlash_boot_lower <- apply(backlash_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
backlash_boot_upper <- apply(backlash_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

disenchantment_boot_mean <- apply(disenchantment_boot, 2, mean, na.rm=TRUE)
disenchantment_boot_lower <- apply(disenchantment_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
disenchantment_boot_upper <- apply(disenchantment_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

# CONSOLIDATE
switching_boot_out <- c(switching_boot_mean, switching_boot_lower, switching_boot_upper)
backlash_boot_out <- c(backlash_boot_mean, backlash_boot_lower, backlash_boot_upper)
disenchantment_boot_out <- c(disenchantment_boot_mean, disenchantment_boot_lower, disenchantment_boot_upper)

df_table <- bind_rows(df_table, data.frame(t(round(100*switching_boot_out, 3)), t(round(100*backlash_boot_out, 3)), t(round(100*disenchantment_boot_out, 3))))
```

## Classification 3: Third-party voters to ABS, June undecided to AKP  
[1] 2.322 1.745 2.919
[1] 3.088 2.608 3.593
[1] -0.242 -0.881  0.397
```{R}
table(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- as.character(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- ifelse(df_surveys$vote_june2019_torake=="Undecided", "AKP", df_surveys$vote_june2019_torake)
table(df_surveys$vote_june2019_torake)
str(df_surveys$vote_june2019_torake)

df_surveys_torake <- df_surveys %>%
  filter(!is.na(dg_sex) & !is.na(dg_age) & !is.na(dg_edu_6)) %>%
  filter(!is.na(vote_2018_torake) & !is.na(vote_march2019_torake) & !is.na(vote_june2019_torake))

df_torake <- svydesign(ids=~1, data=df_surveys_torake)

df_raked <- rake(df_torake, list(~demography_char, ~vote_2018_torake, ~vote_march2019_torake, ~vote_june2019_torake), list(admin_torake, results_2018_torake, results_march2019_torake, results_june2019_torake), control = list(maxit = 100, epsilon = 1, verbose=FALSE))

df_weights <- data.frame(rid = df_surveys_torake$rid, weight_robustness=weights(df_raked)) 

df_surveys_robustness <- df_surveys_torake %>%
  left_join(df_weights)

df_surveys_robustness$vote_march_3 <- fct_recode(df_surveys_robustness$vote_march, 
                            ABS = "THIRD")
df_surveys_robustness$vote_june_3 <- fct_recode(df_surveys_robustness$vote_june, 
                            ABS = "THIRD",
                            AKP ="Undecided")

# Use post-stratification weights as boostrap probabilities
df_toboot <- df_surveys_robustness %>%
  mutate(weight = weight_robustness) %>%
  filter(!is.na(weight)) %>%
  mutate(rid = seq(1:n())) %>%
  select(rid, vote_march_3, vote_june_3, vote_march, vote_june, weight)

# CHECK THAT THE THIRD/UNDECIDED ARE RE-CLASSIFIED AS INTENDED
table(df_toboot$vote_march, df_toboot$vote_march_3, useNA = c("always"))
table(df_toboot$vote_june, df_toboot$vote_june_3, useNA = c("always"))

# BOOTSTRAP 
boot_rep <- 10000
mechs_out <- c()
p_boot_save <- c()
p_counts_boot_save <- c()
for (j in 1:boot_rep) {
  # DRAW RIDS
  rid_boot <- sample(df_toboot$rid, replace=TRUE, prob=df_toboot$weight)
  # EXTRACT THE DATA BASED ON THE DRAW
  df_boot <- df_toboot[rid_boot,]
  # THE CROSSTAB
  p_counts_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)
  # COMPUTE THE JOINT PMF
  p_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)/sum(table(df_boot$vote_march_3, df_boot$vote_june_3))
  # TRANSFORM TO A VECTOR THAT IS A ROWWISE TRANSFORMATION OF THE MATRIX
  p_counts_boot <- c(t(p_counts_boot))
  p_boot <- c(t(p_boot))
  # SAVE IT
  p_counts_boot_save <- rbind(p_counts_boot_save, p_counts_boot)
  p_boot_save <- rbind(p_boot_save, p_boot)
}
  
### Compute the mechanisms: the JOINT PMF
switching_boot <- c()
backlash_boot <- c()
disenchantment_boot <- c()
  
for (k in 1:boot_rep) {
  P <- matrix(p_boot_save[k,], nrow=3, byrow=TRUE)
  
  switching <- P[1,2] - P[2,1]
  switching_boot <- rbind(switching_boot, switching)
  
  backlash <- P[3,2] - P[2,3]
  backlash_boot <- rbind(backlash_boot, backlash)
  
  disenchantment <- P[1,3] - P[3,1]
  disenchantment_boot <- rbind(disenchantment_boot, disenchantment)
}

switching_boot_mean <- apply(switching_boot, 2, mean, na.rm=TRUE)
switching_boot_lower <- apply(switching_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
switching_boot_upper <- apply(switching_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

backlash_boot_mean <- apply(backlash_boot, 2, mean, na.rm=TRUE)
backlash_boot_lower <- apply(backlash_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
backlash_boot_upper <- apply(backlash_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

disenchantment_boot_mean <- apply(disenchantment_boot, 2, mean, na.rm=TRUE)
disenchantment_boot_lower <- apply(disenchantment_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
disenchantment_boot_upper <- apply(disenchantment_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

# CONSOLIDATE
switching_boot_out <- c(switching_boot_mean, switching_boot_lower, switching_boot_upper)
backlash_boot_out <- c(backlash_boot_mean, backlash_boot_lower, backlash_boot_upper)
disenchantment_boot_out <- c(disenchantment_boot_mean, disenchantment_boot_lower, disenchantment_boot_upper)

df_table <- bind_rows(df_table, data.frame(t(round(100*switching_boot_out, 3)), t(round(100*backlash_boot_out, 3)), t(round(100*disenchantment_boot_out, 3))))
```

## Classification 4: Third-party voters to ABS, June undecided to CHP  
```{R}
table(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- as.character(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- ifelse(df_surveys$vote_june2019_torake=="Undecided", "CHP", df_surveys$vote_june2019_torake)
table(df_surveys$vote_june2019_torake)
str(df_surveys$vote_june2019_torake)

df_surveys_torake <- df_surveys %>%
  filter(!is.na(dg_sex) & !is.na(dg_age) & !is.na(dg_edu_6)) %>%
  filter(!is.na(vote_2018_torake) & !is.na(vote_march2019_torake) & !is.na(vote_june2019_torake))

df_torake <- svydesign(ids=~1, data=df_surveys_torake)

df_raked <- rake(df_torake, list(~demography_char, ~vote_2018_torake, ~vote_march2019_torake, ~vote_june2019_torake), list(admin_torake, results_2018_torake, results_march2019_torake, results_june2019_torake), control = list(maxit = 100, epsilon = 1, verbose=FALSE))

df_weights <- data.frame(rid = df_surveys_torake$rid, weight_robustness=weights(df_raked)) 

df_surveys_robustness <- df_surveys_torake %>%
  left_join(df_weights)

df_surveys_robustness$vote_march_3 <- fct_recode(df_surveys_robustness$vote_march, 
                            ABS = "THIRD")
df_surveys_robustness$vote_june_3 <- fct_recode(df_surveys_robustness$vote_june, 
                            ABS = "THIRD",
                            CHP ="Undecided")

# Use post-stratification weights as boostrap probabilities
df_toboot <- df_surveys_robustness %>%
  mutate(weight = weight_robustness) %>%
  filter(!is.na(weight)) %>%
  mutate(rid = seq(1:n())) %>%
  select(rid, vote_march_3, vote_june_3, vote_march, vote_june, weight)

# CHECK THAT THE THIRD/UNDECIDED ARE RE-CLASSIFIED AS INTENDED
table(df_toboot$vote_march, df_toboot$vote_march_3, useNA = c("always"))
table(df_toboot$vote_june, df_toboot$vote_june_3, useNA = c("always"))

# BOOTSTRAP 
boot_rep <- 10000
mechs_out <- c()
p_boot_save <- c()
p_counts_boot_save <- c()
for (j in 1:boot_rep) {
  # DRAW RIDS
  rid_boot <- sample(df_toboot$rid, replace=TRUE, prob=df_toboot$weight)
  # EXTRACT THE DATA BASED ON THE DRAW
  df_boot <- df_toboot[rid_boot,]
  # THE CROSSTAB
  p_counts_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)
  # COMPUTE THE JOINT PMF
  p_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)/sum(table(df_boot$vote_march_3, df_boot$vote_june_3))
  # TRANSFORM TO A VECTOR THAT IS A ROWWISE TRANSFORMATION OF THE MATRIX
  p_counts_boot <- c(t(p_counts_boot))
  p_boot <- c(t(p_boot))
  # SAVE IT
  p_counts_boot_save <- rbind(p_counts_boot_save, p_counts_boot)
  p_boot_save <- rbind(p_boot_save, p_boot)
}
  
### Compute the mechanisms: the JOINT PMF
switching_boot <- c()
backlash_boot <- c()
disenchantment_boot <- c()
  
for (k in 1:boot_rep) {
  P <- matrix(p_boot_save[k,], nrow=3, byrow=TRUE)
  
  switching <- P[1,2] - P[2,1]
  switching_boot <- rbind(switching_boot, switching)
  
  backlash <- P[3,2] - P[2,3]
  backlash_boot <- rbind(backlash_boot, backlash)
  
  disenchantment <- P[1,3] - P[3,1]
  disenchantment_boot <- rbind(disenchantment_boot, disenchantment)
}

switching_boot_mean <- apply(switching_boot, 2, mean, na.rm=TRUE)
switching_boot_lower <- apply(switching_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
switching_boot_upper <- apply(switching_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

backlash_boot_mean <- apply(backlash_boot, 2, mean, na.rm=TRUE)
backlash_boot_lower <- apply(backlash_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
backlash_boot_upper <- apply(backlash_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

disenchantment_boot_mean <- apply(disenchantment_boot, 2, mean, na.rm=TRUE)
disenchantment_boot_lower <- apply(disenchantment_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
disenchantment_boot_upper <- apply(disenchantment_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

# CONSOLIDATE
switching_boot_out <- c(switching_boot_mean, switching_boot_lower, switching_boot_upper)
backlash_boot_out <- c(backlash_boot_mean, backlash_boot_lower, backlash_boot_upper)
disenchantment_boot_out <- c(disenchantment_boot_mean, disenchantment_boot_lower, disenchantment_boot_upper)

df_table <- bind_rows(df_table, data.frame(t(round(100*switching_boot_out, 3)), t(round(100*backlash_boot_out, 3)), t(round(100*disenchantment_boot_out, 3))))
```

## Classification 5: Third-party voters differentiated, June undecided dropped
```{R}
table(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- as.character(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- ifelse(df_surveys$vote_june2019_torake=="Undecided", NA, df_surveys$vote_june2019_torake)
table(df_surveys$vote_june2019_torake)
str(df_surveys$vote_june2019_torake)

df_surveys_torake <- df_surveys %>%
  filter(!is.na(dg_sex) & !is.na(dg_age) & !is.na(dg_edu_6)) %>%
  filter(!is.na(vote_2018_torake) & !is.na(vote_march2019_torake) & !is.na(vote_june2019_torake))

df_torake <- svydesign(ids=~1, data=df_surveys_torake)

df_raked <- rake(df_torake, list(~demography_char, ~vote_2018_torake, ~vote_march2019_torake, ~vote_june2019_torake), list(admin_torake, results_2018_torake, results_march2019_torake, results_june2019_torake), control = list(maxit = 100, epsilon = 1, verbose=FALSE))

df_weights <- data.frame(rid = df_surveys_torake$rid, weight_robustness=weights(df_raked)) 

df_surveys_robustness <- df_surveys_torake %>%
  left_join(df_weights)

df_surveys_robustness$vote_march_3 <- fct_recode(df_surveys_robustness$vote_march, 
                            THIRD = "THIRD")
df_surveys_robustness$vote_june_3 <- fct_recode(df_surveys_robustness$vote_june, 
                            THIRD = "THIRD",
                            NULL ="Undecided")

# Use post-stratification weights as boostrap probabilities
df_toboot <- df_surveys_robustness %>%
  mutate(weight = weight_robustness) %>%
  filter(!is.na(weight)) %>%
  mutate(rid = seq(1:n())) %>%
  select(rid, vote_march_3, vote_june_3, vote_march, vote_june, weight)

# CHECK THAT THE THIRD/UNDECIDED ARE RE-CLASSIFIED AS INTENDED
table(df_toboot$vote_march, df_toboot$vote_march_3, useNA = c("always"))
table(df_toboot$vote_june, df_toboot$vote_june_3, useNA = c("always"))

# Reorder vote choices to: AKP, CHP, ABS, THIRD
df_toboot <- df_toboot %>%
  mutate(vote_march_3 = factor(vote_march_3, levels=c("AKP", "CHP", "ABS", "THIRD"))) %>%
  mutate(vote_june_3 = factor(vote_june_3, levels=c("AKP", "CHP", "ABS", "THIRD")))

table(df_toboot$vote_march_3)
table(df_toboot$vote_june_3)

# BOOTSTRAP 
boot_rep <- 10000
mechs_out <- c()
p_boot_save <- c()
p_counts_boot_save <- c()
for (j in 1:boot_rep) {
  # DRAW RIDS
  rid_boot <- sample(df_toboot$rid, replace=TRUE, prob=df_toboot$weight)
  # EXTRACT THE DATA BASED ON THE DRAW
  df_boot <- df_toboot[rid_boot,]
  # THE CROSSTAB
  p_counts_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)
  # COMPUTE THE JOINT PMF
  p_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)/sum(table(df_boot$vote_march_3, df_boot$vote_june_3))
  # TRANSFORM TO A VECTOR THAT IS A ROWWISE TRANSFORMATION OF THE MATRIX
  p_counts_boot <- c(t(p_counts_boot))
  p_boot <- c(t(p_boot))
  # SAVE IT
  p_counts_boot_save <- rbind(p_counts_boot_save, p_counts_boot)
  p_boot_save <- rbind(p_boot_save, p_boot)
}
  
### Compute the mechanisms: the JOINT PMF
switching_boot <- c()
backlash_boot <- c()
disenchantment_boot <- c()
  
for (k in 1:boot_rep) {
  P <- matrix(p_boot_save[k,], nrow=4, byrow=TRUE)
  
  switching <- P[1,2] - P[2,1]
  switching_boot <- rbind(switching_boot, switching)
  
  backlash <- P[3,2] - P[2,3]
  backlash_boot <- rbind(backlash_boot, backlash)
  
  disenchantment <- P[1,3] - P[3,1]
  disenchantment_boot <- rbind(disenchantment_boot, disenchantment)
}

switching_boot_mean <- apply(switching_boot, 2, mean, na.rm=TRUE)
switching_boot_lower <- apply(switching_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
switching_boot_upper <- apply(switching_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

backlash_boot_mean <- apply(backlash_boot, 2, mean, na.rm=TRUE)
backlash_boot_lower <- apply(backlash_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
backlash_boot_upper <- apply(backlash_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

disenchantment_boot_mean <- apply(disenchantment_boot, 2, mean, na.rm=TRUE)
disenchantment_boot_lower <- apply(disenchantment_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
disenchantment_boot_upper <- apply(disenchantment_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

# CONSOLIDATE
switching_boot_out <- c(switching_boot_mean, switching_boot_lower, switching_boot_upper)
backlash_boot_out <- c(backlash_boot_mean, backlash_boot_lower, backlash_boot_upper)
disenchantment_boot_out <- c(disenchantment_boot_mean, disenchantment_boot_lower, disenchantment_boot_upper)

df_table <- bind_rows(df_table, data.frame(t(round(100*switching_boot_out, 3)), t(round(100*backlash_boot_out, 3)), t(round(100*disenchantment_boot_out, 3))))
```

### Classification 6: Third-party voters differentiated, June undecided to ABS
```{R}
table(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- as.character(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- ifelse(df_surveys$vote_june2019_torake=="Undecided", "ABS", df_surveys$vote_june2019_torake)
table(df_surveys$vote_june2019_torake)
str(df_surveys$vote_june2019_torake)

df_surveys_torake <- df_surveys %>%
  filter(!is.na(dg_sex) & !is.na(dg_age) & !is.na(dg_edu_6)) %>%
  filter(!is.na(vote_2018_torake) & !is.na(vote_march2019_torake) & !is.na(vote_june2019_torake))

df_torake <- svydesign(ids=~1, data=df_surveys_torake)

df_raked <- rake(df_torake, list(~demography_char, ~vote_2018_torake, ~vote_march2019_torake, ~vote_june2019_torake), list(admin_torake, results_2018_torake, results_march2019_torake, results_june2019_torake), control = list(maxit = 100, epsilon = 1, verbose=FALSE))

df_weights <- data.frame(rid = df_surveys_torake$rid, weight_robustness=weights(df_raked)) 

df_surveys_robustness <- df_surveys_torake %>%
  left_join(df_weights)

df_surveys_robustness$vote_march_3 <- fct_recode(df_surveys_robustness$vote_march, 
                            THIRD = "THIRD")
df_surveys_robustness$vote_june_3 <- fct_recode(df_surveys_robustness$vote_june, 
                            THIRD = "THIRD",
                            ABS ="Undecided")

# Use post-stratification weights as boostrap probabilities
df_toboot <- df_surveys_robustness %>%
  mutate(weight = weight_robustness) %>%
  filter(!is.na(weight)) %>%
  mutate(rid = seq(1:n())) %>%
  select(rid, vote_march_3, vote_june_3, vote_march, vote_june, weight)

# CHECK THAT THE THIRD/UNDECIDED ARE RE-CLASSIFIED AS INTENDED
table(df_toboot$vote_march, df_toboot$vote_march_3, useNA = c("always"))
table(df_toboot$vote_june, df_toboot$vote_june_3, useNA = c("always"))

# Reorder vote choices to: AKP, CHP, ABS, THIRD
df_toboot <- df_toboot %>%
  mutate(vote_march_3 = factor(vote_march_3, levels=c("AKP", "CHP", "ABS", "THIRD"))) %>%
  mutate(vote_june_3 = factor(vote_june_3, levels=c("AKP", "CHP", "ABS", "THIRD")))

table(df_toboot$vote_march_3)
table(df_toboot$vote_june_3)

# BOOTSTRAP 
boot_rep <- 10000
mechs_out <- c()
p_boot_save <- c()
p_counts_boot_save <- c()
for (j in 1:boot_rep) {
  # DRAW RIDS
  rid_boot <- sample(df_toboot$rid, replace=TRUE, prob=df_toboot$weight)
  # EXTRACT THE DATA BASED ON THE DRAW
  df_boot <- df_toboot[rid_boot,]
  # THE CROSSTAB
  p_counts_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)
  # COMPUTE THE JOINT PMF
  p_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)/sum(table(df_boot$vote_march_3, df_boot$vote_june_3))
  # TRANSFORM TO A VECTOR THAT IS A ROWWISE TRANSFORMATION OF THE MATRIX
  p_counts_boot <- c(t(p_counts_boot))
  p_boot <- c(t(p_boot))
  # SAVE IT
  p_counts_boot_save <- rbind(p_counts_boot_save, p_counts_boot)
  p_boot_save <- rbind(p_boot_save, p_boot)
}
  
### Compute the mechanisms: the JOINT PMF
switching_boot <- c()
backlash_boot <- c()
disenchantment_boot <- c()
  
for (k in 1:boot_rep) {
  P <- matrix(p_boot_save[k,], nrow=4, byrow=TRUE)
  
  switching <- P[1,2] - P[2,1]
  switching_boot <- rbind(switching_boot, switching)
  
  backlash <- P[3,2] - P[2,3]
  backlash_boot <- rbind(backlash_boot, backlash)
  
  disenchantment <- P[1,3] - P[3,1]
  disenchantment_boot <- rbind(disenchantment_boot, disenchantment)
}

switching_boot_mean <- apply(switching_boot, 2, mean, na.rm=TRUE)
switching_boot_lower <- apply(switching_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
switching_boot_upper <- apply(switching_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

backlash_boot_mean <- apply(backlash_boot, 2, mean, na.rm=TRUE)
backlash_boot_lower <- apply(backlash_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
backlash_boot_upper <- apply(backlash_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

disenchantment_boot_mean <- apply(disenchantment_boot, 2, mean, na.rm=TRUE)
disenchantment_boot_lower <- apply(disenchantment_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
disenchantment_boot_upper <- apply(disenchantment_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

# CONSOLIDATE
switching_boot_out <- c(switching_boot_mean, switching_boot_lower, switching_boot_upper)
backlash_boot_out <- c(backlash_boot_mean, backlash_boot_lower, backlash_boot_upper)
disenchantment_boot_out <- c(disenchantment_boot_mean, disenchantment_boot_lower, disenchantment_boot_upper)

df_table <- bind_rows(df_table, data.frame(t(round(100*switching_boot_out, 3)), t(round(100*backlash_boot_out, 3)), t(round(100*disenchantment_boot_out, 3))))
```

### Classification 7: Third-party voters differentiated, June undecided to AKP
```{R}
table(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- as.character(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- ifelse(df_surveys$vote_june2019_torake=="Undecided", "AKP", df_surveys$vote_june2019_torake)
table(df_surveys$vote_june2019_torake)
str(df_surveys$vote_june2019_torake)

df_surveys_torake <- df_surveys %>%
  filter(!is.na(dg_sex) & !is.na(dg_age) & !is.na(dg_edu_6)) %>%
  filter(!is.na(vote_2018_torake) & !is.na(vote_march2019_torake) & !is.na(vote_june2019_torake))

df_torake <- svydesign(ids=~1, data=df_surveys_torake)

df_raked <- rake(df_torake, list(~demography_char, ~vote_2018_torake, ~vote_march2019_torake, ~vote_june2019_torake), list(admin_torake, results_2018_torake, results_march2019_torake, results_june2019_torake), control = list(maxit = 100, epsilon = 1, verbose=FALSE))

df_weights <- data.frame(rid = df_surveys_torake$rid, weight_robustness=weights(df_raked)) 

df_surveys_robustness <- df_surveys_torake %>%
  left_join(df_weights)

df_surveys_robustness$vote_march_3 <- fct_recode(df_surveys_robustness$vote_march, 
                            THIRD = "THIRD")
df_surveys_robustness$vote_june_3 <- fct_recode(df_surveys_robustness$vote_june, 
                            THIRD = "THIRD",
                            AKP ="Undecided")

# Use post-stratification weights as boostrap probabilities
df_toboot <- df_surveys_robustness %>%
  mutate(weight = weight_robustness) %>%
  filter(!is.na(weight)) %>%
  mutate(rid = seq(1:n())) %>%
  select(rid, vote_march_3, vote_june_3, vote_march, vote_june, weight)

# CHECK THAT THE THIRD/UNDECIDED ARE RE-CLASSIFIED AS INTENDED
table(df_toboot$vote_march, df_toboot$vote_march_3, useNA = c("always"))
table(df_toboot$vote_june, df_toboot$vote_june_3, useNA = c("always"))

# Reorder vote choices to: AKP, CHP, ABS, THIRD
df_toboot <- df_toboot %>%
  mutate(vote_march_3 = factor(vote_march_3, levels=c("AKP", "CHP", "ABS", "THIRD"))) %>%
  mutate(vote_june_3 = factor(vote_june_3, levels=c("AKP", "CHP", "ABS", "THIRD")))

table(df_toboot$vote_march_3)
table(df_toboot$vote_june_3)

# BOOTSTRAP 
boot_rep <- 10000
mechs_out <- c()
p_boot_save <- c()
p_counts_boot_save <- c()
for (j in 1:boot_rep) {
  # DRAW RIDS
  rid_boot <- sample(df_toboot$rid, replace=TRUE, prob=df_toboot$weight)
  # EXTRACT THE DATA BASED ON THE DRAW
  df_boot <- df_toboot[rid_boot,]
  # THE CROSSTAB
  p_counts_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)
  # COMPUTE THE JOINT PMF
  p_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)/sum(table(df_boot$vote_march_3, df_boot$vote_june_3))
  # TRANSFORM TO A VECTOR THAT IS A ROWWISE TRANSFORMATION OF THE MATRIX
  p_counts_boot <- c(t(p_counts_boot))
  p_boot <- c(t(p_boot))
  # SAVE IT
  p_counts_boot_save <- rbind(p_counts_boot_save, p_counts_boot)
  p_boot_save <- rbind(p_boot_save, p_boot)
}
  
### Compute the mechanisms: the JOINT PMF
switching_boot <- c()
backlash_boot <- c()
disenchantment_boot <- c()
  
for (k in 1:boot_rep) {
  P <- matrix(p_boot_save[k,], nrow=4, byrow=TRUE)
  
  switching <- P[1,2] - P[2,1]
  switching_boot <- rbind(switching_boot, switching)
  
  backlash <- P[3,2] - P[2,3]
  backlash_boot <- rbind(backlash_boot, backlash)
  
  disenchantment <- P[1,3] - P[3,1]
  disenchantment_boot <- rbind(disenchantment_boot, disenchantment)
}

switching_boot_mean <- apply(switching_boot, 2, mean, na.rm=TRUE)
switching_boot_lower <- apply(switching_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
switching_boot_upper <- apply(switching_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

backlash_boot_mean <- apply(backlash_boot, 2, mean, na.rm=TRUE)
backlash_boot_lower <- apply(backlash_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
backlash_boot_upper <- apply(backlash_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

disenchantment_boot_mean <- apply(disenchantment_boot, 2, mean, na.rm=TRUE)
disenchantment_boot_lower <- apply(disenchantment_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
disenchantment_boot_upper <- apply(disenchantment_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

# CONSOLIDATE
switching_boot_out <- c(switching_boot_mean, switching_boot_lower, switching_boot_upper)
backlash_boot_out <- c(backlash_boot_mean, backlash_boot_lower, backlash_boot_upper)
disenchantment_boot_out <- c(disenchantment_boot_mean, disenchantment_boot_lower, disenchantment_boot_upper)

df_table <- bind_rows(df_table, data.frame(t(round(100*switching_boot_out, 3)), t(round(100*backlash_boot_out, 3)), t(round(100*disenchantment_boot_out, 3))))
```

### Classification 8: Third-party voters differentiated, June undecided to CHP
```{R}
table(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- as.character(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- ifelse(df_surveys$vote_june2019_torake=="Undecided", "CHP", df_surveys$vote_june2019_torake)
table(df_surveys$vote_june2019_torake)
str(df_surveys$vote_june2019_torake)

df_surveys_torake <- df_surveys %>%
  filter(!is.na(dg_sex) & !is.na(dg_age) & !is.na(dg_edu_6)) %>%
  filter(!is.na(vote_2018_torake) & !is.na(vote_march2019_torake) & !is.na(vote_june2019_torake))

df_torake <- svydesign(ids=~1, data=df_surveys_torake)

df_raked <- rake(df_torake, list(~demography_char, ~vote_2018_torake, ~vote_march2019_torake, ~vote_june2019_torake), list(admin_torake, results_2018_torake, results_march2019_torake, results_june2019_torake), control = list(maxit = 100, epsilon = 1, verbose=FALSE))

df_weights <- data.frame(rid = df_surveys_torake$rid, weight_robustness=weights(df_raked)) 

df_surveys_robustness <- df_surveys_torake %>%
  left_join(df_weights)

df_surveys_robustness$vote_march_3 <- fct_recode(df_surveys_robustness$vote_march, 
                            THIRD = "THIRD")
df_surveys_robustness$vote_june_3 <- fct_recode(df_surveys_robustness$vote_june, 
                            THIRD = "THIRD",
                            CHP ="Undecided")

# Use post-stratification weights as boostrap probabilities
df_toboot <- df_surveys_robustness %>%
  mutate(weight = weight_robustness) %>%
  filter(!is.na(weight)) %>%
  mutate(rid = seq(1:n())) %>%
  select(rid, vote_march_3, vote_june_3, vote_march, vote_june, weight)

# CHECK THAT THE THIRD/UNDECIDED ARE RE-CLASSIFIED AS INTENDED
table(df_toboot$vote_march, df_toboot$vote_march_3, useNA = c("always"))
table(df_toboot$vote_june, df_toboot$vote_june_3, useNA = c("always"))

# Reorder vote choices to: AKP, CHP, ABS, THIRD
df_toboot <- df_toboot %>%
  mutate(vote_march_3 = factor(vote_march_3, levels=c("AKP", "CHP", "ABS", "THIRD"))) %>%
  mutate(vote_june_3 = factor(vote_june_3, levels=c("AKP", "CHP", "ABS", "THIRD")))

table(df_toboot$vote_march_3)
table(df_toboot$vote_june_3)

# BOOTSTRAP 
boot_rep <- 10000
mechs_out <- c()
p_boot_save <- c()
p_counts_boot_save <- c()
for (j in 1:boot_rep) {
  # DRAW RIDS
  rid_boot <- sample(df_toboot$rid, replace=TRUE, prob=df_toboot$weight)
  # EXTRACT THE DATA BASED ON THE DRAW
  df_boot <- df_toboot[rid_boot,]
  # THE CROSSTAB
  p_counts_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)
  # COMPUTE THE JOINT PMF
  p_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)/sum(table(df_boot$vote_march_3, df_boot$vote_june_3))
  # TRANSFORM TO A VECTOR THAT IS A ROWWISE TRANSFORMATION OF THE MATRIX
  p_counts_boot <- c(t(p_counts_boot))
  p_boot <- c(t(p_boot))
  # SAVE IT
  p_counts_boot_save <- rbind(p_counts_boot_save, p_counts_boot)
  p_boot_save <- rbind(p_boot_save, p_boot)
}
  
### Compute the mechanisms: the JOINT PMF
switching_boot <- c()
backlash_boot <- c()
disenchantment_boot <- c()
  
for (k in 1:boot_rep) {
  P <- matrix(p_boot_save[k,], nrow=4, byrow=TRUE)
  
  switching <- P[1,2] - P[2,1]
  switching_boot <- rbind(switching_boot, switching)
  
  backlash <- P[3,2] - P[2,3]
  backlash_boot <- rbind(backlash_boot, backlash)
  
  disenchantment <- P[1,3] - P[3,1]
  disenchantment_boot <- rbind(disenchantment_boot, disenchantment)
}

switching_boot_mean <- apply(switching_boot, 2, mean, na.rm=TRUE)
switching_boot_lower <- apply(switching_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
switching_boot_upper <- apply(switching_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

backlash_boot_mean <- apply(backlash_boot, 2, mean, na.rm=TRUE)
backlash_boot_lower <- apply(backlash_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
backlash_boot_upper <- apply(backlash_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

disenchantment_boot_mean <- apply(disenchantment_boot, 2, mean, na.rm=TRUE)
disenchantment_boot_lower <- apply(disenchantment_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
disenchantment_boot_upper <- apply(disenchantment_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

# CONSOLIDATE
switching_boot_out <- c(switching_boot_mean, switching_boot_lower, switching_boot_upper)
backlash_boot_out <- c(backlash_boot_mean, backlash_boot_lower, backlash_boot_upper)
disenchantment_boot_out <- c(disenchantment_boot_mean, disenchantment_boot_lower, disenchantment_boot_upper)

df_table <- bind_rows(df_table, data.frame(t(round(100*switching_boot_out, 3)), t(round(100*backlash_boot_out, 3)), t(round(100*disenchantment_boot_out, 3))))
```

### Classification 9: Third-party voters differentiated, June undecided to THIRD
```{R}
table(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- as.character(df_surveys$vote_june)
df_surveys$vote_june2019_torake <- ifelse(df_surveys$vote_june2019_torake=="Undecided", "THIRD", df_surveys$vote_june2019_torake)
table(df_surveys$vote_june2019_torake)
str(df_surveys$vote_june2019_torake)

df_surveys_torake <- df_surveys %>%
  filter(!is.na(dg_sex) & !is.na(dg_age) & !is.na(dg_edu_6)) %>%
  filter(!is.na(vote_2018_torake) & !is.na(vote_march2019_torake) & !is.na(vote_june2019_torake))

df_torake <- svydesign(ids=~1, data=df_surveys_torake)

df_raked <- rake(df_torake, list(~demography_char, ~vote_2018_torake, ~vote_march2019_torake, ~vote_june2019_torake), list(admin_torake, results_2018_torake, results_march2019_torake, results_june2019_torake), control = list(maxit = 100, epsilon = 1, verbose=FALSE))

df_weights <- data.frame(rid = df_surveys_torake$rid, weight_robustness=weights(df_raked)) 

df_surveys_robustness <- df_surveys_torake %>%
  left_join(df_weights)

df_surveys_robustness$vote_march_3 <- fct_recode(df_surveys_robustness$vote_march, 
                            THIRD = "THIRD")
df_surveys_robustness$vote_june_3 <- fct_recode(df_surveys_robustness$vote_june, 
                            THIRD = "THIRD",
                            THIRD ="Undecided")

# Use post-stratification weights as boostrap probabilities
df_toboot <- df_surveys_robustness %>%
  mutate(weight = weight_robustness) %>%
  filter(!is.na(weight)) %>%
  mutate(rid = seq(1:n())) %>%
  select(rid, vote_march_3, vote_june_3, vote_march, vote_june, weight)

# CHECK THAT THE THIRD/UNDECIDED ARE RE-CLASSIFIED AS INTENDED
table(df_toboot$vote_march, df_toboot$vote_march_3, useNA = c("always"))
table(df_toboot$vote_june, df_toboot$vote_june_3, useNA = c("always"))

# Reorder vote choices to: AKP, CHP, ABS, THIRD
df_toboot <- df_toboot %>%
  mutate(vote_march_3 = factor(vote_march_3, levels=c("AKP", "CHP", "ABS", "THIRD"))) %>%
  mutate(vote_june_3 = factor(vote_june_3, levels=c("AKP", "CHP", "ABS", "THIRD")))

table(df_toboot$vote_march_3)
table(df_toboot$vote_june_3)

# BOOTSTRAP 
boot_rep <- 10000
mechs_out <- c()
p_boot_save <- c()
p_counts_boot_save <- c()
for (j in 1:boot_rep) {
  # DRAW RIDS
  rid_boot <- sample(df_toboot$rid, replace=TRUE, prob=df_toboot$weight)
  # EXTRACT THE DATA BASED ON THE DRAW
  df_boot <- df_toboot[rid_boot,]
  # THE CROSSTAB
  p_counts_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)
  # COMPUTE THE JOINT PMF
  p_boot <- table(df_boot$vote_march_3, df_boot$vote_june_3)/sum(table(df_boot$vote_march_3, df_boot$vote_june_3))
  # TRANSFORM TO A VECTOR THAT IS A ROWWISE TRANSFORMATION OF THE MATRIX
  p_counts_boot <- c(t(p_counts_boot))
  p_boot <- c(t(p_boot))
  # SAVE IT
  p_counts_boot_save <- rbind(p_counts_boot_save, p_counts_boot)
  p_boot_save <- rbind(p_boot_save, p_boot)
}
  
### Compute the mechanisms: the JOINT PMF
switching_boot <- c()
backlash_boot <- c()
disenchantment_boot <- c()
  
for (k in 1:boot_rep) {
  P <- matrix(p_boot_save[k,], nrow=4, byrow=TRUE)
  
  switching <- P[1,2] - P[2,1]
  switching_boot <- rbind(switching_boot, switching)
  
  backlash <- P[3,2] - P[2,3]
  backlash_boot <- rbind(backlash_boot, backlash)
  
  disenchantment <- P[1,3] - P[3,1]
  disenchantment_boot <- rbind(disenchantment_boot, disenchantment)
}

switching_boot_mean <- apply(switching_boot, 2, mean, na.rm=TRUE)
switching_boot_lower <- apply(switching_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
switching_boot_upper <- apply(switching_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

backlash_boot_mean <- apply(backlash_boot, 2, mean, na.rm=TRUE)
backlash_boot_lower <- apply(backlash_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
backlash_boot_upper <- apply(backlash_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

disenchantment_boot_mean <- apply(disenchantment_boot, 2, mean, na.rm=TRUE)
disenchantment_boot_lower <- apply(disenchantment_boot, 2, quantile, probs=c(.025), na.rm=TRUE)
disenchantment_boot_upper <- apply(disenchantment_boot, 2, quantile, probs=c(.975), na.rm=TRUE)

# CONSOLIDATE
switching_boot_out <- c(switching_boot_mean, switching_boot_lower, switching_boot_upper)
backlash_boot_out <- c(backlash_boot_mean, backlash_boot_lower, backlash_boot_upper)
disenchantment_boot_out <- c(disenchantment_boot_mean, disenchantment_boot_lower, disenchantment_boot_upper)

df_table <- bind_rows(df_table, data.frame(t(round(100*switching_boot_out, 3)), t(round(100*backlash_boot_out, 3)), t(round(100*disenchantment_boot_out, 3))))
```

### Report Table 5.3
```{R}
colnames(df_table) <- c("Vote Switching", "(", ")", "Backlash", "(", ")", "Disenchantment", "(", ")")
df_table
```

